﻿using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using mojoPortal.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.mojoPortalExtend {
    class OwnedPageEditSettingsLink : HyperLink {

        private PageSettings currentPage;

        private bool renderAsListItem = false;
        public bool RenderAsListItem {
            get { return renderAsListItem; }
            set { renderAsListItem = value; }
        }

        private string listItemCSS = string.Empty;
        public string ListItemCss {
            get { return listItemCSS; }
            set { listItemCSS = value; }
        }

        private bool ShouldRender() {
            if (this.currentPage == null) { return false; }
            if (!Page.Request.IsAuthenticated) { return false; }
            if (this.currentPage == null) { return false; }
            if (this.currentPage.PageId == -1) { return false; }

            if (this.currentPage.EditRoles == "Admins;") {
                if (!WebUser.IsAdmin) { return false; }
            }

            if (WebUser.IsAdminOrContentAdmin) { return true; }

            if (
                (WebUser.IsInRoles(this.currentPage.EditRoles))
                || (this.currentPage.IsPending && WebUser.IsInRoles(this.currentPage.DraftEditOnlyRoles))
                ) {
                return true;
            }

            if (SiteUtils.UserIsSiteEditor()) { return true; }

            //if (!WebConfigSettings.UseRelatedSiteMode) { return false; }

            //if (basePage.SiteInfo == null) { return false; }
            //// in related sites mode usersin site eidotrs role can use admin menu
            //if (WebUser.IsInRoles(basePage.SiteInfo.SiteRootEditRoles)) { return true; }

            return false;

        }

        public OwnedPageEditSettingsLink() { }

        public OwnedPageEditSettingsLink(PageSettings page) {
            this.currentPage = page;
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            if (HttpContext.Current == null) { return; }
            EnableViewState = false;

            if (this.currentPage == null && this.Page is mojoBasePage)
                this.currentPage = (Page as CmsPage).CurrentPage;

            Visible = ShouldRender();
            if (!Visible) { return; }

            if (CssClass.Length > 0) {
                CssClass = "adminlink pagesettingslink " + CssClass;
            }
            else {
                CssClass = "adminlink pagesettingslink";
            }

            ToolTip = "Edytuj ustawienia strony";

            if (SiteUtils.SslIsAvailable()) {
                if (this.currentPage != null) {
                    NavigateUrl = SiteUtils.GetNavigationSiteRoot() + "/Admin/PageSettings.aspx?pageid=" + this.currentPage.PageId;
                }
            }
            else {
                if (this.currentPage != null) {
                    NavigateUrl = SiteUtils.GetNavigationSiteRoot() + "/Admin/PageSettings.aspx?pageid=" + this.currentPage.PageId;
                }

            }

            if (Page is mojoBasePage && (Page as mojoBasePage).UseIconsForAdminLinks) {
                ImageUrl = Page.ResolveUrl("~/Data/SiteImages/" + WebConfigSettings.EditPageSettingsImage);
                Text = "Edytuj ustawienia strony";
            }
            else {
                Text = "Ustawienia strony";
            }
        }

        protected override void Render(HtmlTextWriter writer) {
            if (HttpContext.Current == null) {
                writer.Write("[" + this.ID + "]");
                return;
            }

            if (renderAsListItem) {
                if (listItemCSS.Length > 0) {
                    writer.Write("<li class='" + listItemCSS + "'>");
                }
                else {
                    writer.Write("<li>");
                }
            }

            base.Render(writer);

            if (renderAsListItem) {
                writer.Write("</li>");
            }
        }

    }
}
